//
//  SendPhotoViewModel.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/15.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit
import HXPhotoPicker

class SendPhotoViewModel: NSObject {

    var mananger:HXPhotoManager
    var status: Int = 0
    var remark:String?
    
    
    var max:Int {
        
        return 9 - mananger.afterSelectedCount()
    }
    
    var type: SelectType {
        
        if mananger.afterSelectedCount() == 0 {
            
            return .all
        } else {
            
            return .photo
        }
    }
    
    
    var showToastActivity: ((Bool) -> Void)?
    var loadFinished: ((_ error: String?) -> Void)?
    
    
    override init() {
        
        mananger = HXPhotoManager(type: .photoAndVideo)
        mananger.configuration.videoCanEdit = false
        mananger.configuration.specialModeNeedHideVideoSelectBtn = true
        
        mananger.configuration.statusBarStyle = .default
        
        super.init()
    }
    
    func addModels(models:[HXPhotoModel], type:SelectType) {
        
        if models.count > 0 {
            mananger.addModelArray(models)
        }
        
        if type == .video {
            
            mananger.configuration.maxNum = 1
        } else {
            
            mananger.configuration.maxNum = 9
        }
    }
    
    
    func uploadBlog(remark:String?) {
        
        self.remark = remark
        
        guard let picArray = mananger.afterSelectedArray() as? [HXPhotoModel] else {
            
            loadFinished?("至少选择一张图片")
            
            return
        }
        
        if picArray.count == 0 {
            
            loadFinished?("至少选择一张图片")
            return
        }
        
        showToastActivity?(true);
        
        NetAPI.uploadToQN(picArray) {
            
            [weak self] isVideo, keys, errmsg, size in
            guard let `self` = self else { return }
            
            if let msg = errmsg {
                
                self.showToastActivity?(false);
                self.loadFinished?(msg)
                
                return
            }
            
            if isVideo {
                
                self.uploadVideo(key: (keys?.first)!, size: size!)
            } else {
                
                self.uploadPhoto(keys: keys!)
            }
            
        }
    }
    
    private func uploadVideo(key:String,size:CGSize) {
        
        let params:[String :Any] = ["videos":[["video_url":key,"width":size.width,"height":size.height]],"remark":remark ?? "","status":status]
        
        NetAPI.postBlog(isVideo: true, params: params).request {
            
            [weak self] response, errmsg in
            guard let `self` = self else { return }
            
            self.uploadFinished(errmsg)
        }
    }
    
    private func uploadPhoto(keys:[String]) {
        
        let params:[String :Any] = ["pictures":keys,"remark":remark ?? "","status":status]
        
        NetAPI.postBlog(isVideo: false, params: params).request {
            
            [weak self] response, errmsg in
            guard let `self` = self else { return }
            
            self.uploadFinished(errmsg)
        }
    }
    
    private func uploadFinished(_ errmsg:String?) {
        
        self.showToastActivity?(false);
        
        if let msg = errmsg {
            
            self.loadFinished?(msg)
            return
        }
        
        self.loadFinished?(nil)

    }
}
